package com.kgc.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.api.goods.CarApi;
import com.kgc.api.goods.GoodsItemApi;
import com.kgc.bean.GoodsCarinfo;
import com.kgc.bean.User;
import com.kgc.service.CarService;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class CarServiceImpl implements CarService {
    @Reference(timeout = 30000)
    private CarApi carApi;
    @Reference(timeout = 30000)
    private GoodsItemApi goodsItemApi;



    public ReturnResult<Map<String, Object>> addToShoppingCart(GoodsCarinfo goodsCarinfo, User user, HttpServletRequest request,String goodsId,Integer businessid) {
        log.info("-------------CarServiceImpl____user:{}",user.toString());
        Map<String,Object> returnData=new HashMap<>();
        String token=request.getHeader("token");
        //判断商品是否存在
        if(!goodsItemApi.whetherTheProductExists(goodsCarinfo.getGoodsid())){
            return ResultBuildUtil.fail("3003","该商品不存在");
        }else {
            goodsCarinfo.setBusinessid(goodsItemApi.returnProductInformationAccordingToProductId(goodsCarinfo.getGoodsid()).getShopId());
        }

        return carApi.addToShoppingCart(goodsCarinfo,user,token,goodsId,businessid);
    }

    @Override
    public ReturnResult<Map<String, Object>> checkTheShoppingCart(User user,String goodsId,Integer businessid) {

        return carApi.checkTheShoppingCart(user,goodsId,businessid);
    }

    @Override
    public ReturnResult<Map<String, Object>> deleteAnItemInTheShoppingCart(String goodsId, User user) {
        if(goodsItemApi.returnProductInformationAccordingToProductId(goodsId)==null){
            Map<String,String> resultData=new HashMap<>();
            return ResultBuildUtil.fail("3003","该商品不存在");
        }
        Integer businessid=goodsItemApi.returnProductInformationAccordingToProductId(goodsId).getShopId();

        return carApi.deleteAnItemInTheShoppingCart(goodsId,businessid,user);
    }

    @Override
    public ReturnResult<Map<String, Object>> emptyShoppingCart(User user,String goodsId,Integer businessid) {

        return carApi.emptyShoppingCart(user,goodsId,businessid);
    }




}
